<?php

namespace teamug;


/**
 * Various functions for generating HTML simply, easily
 * 
 */
class Html {
  
  
  /**
   * Generate <option>s
   * 
   * @param mixed $options Assoc array. Or assoc array of assoc arrays when implementing optgroups
   * @param array/string $selected Which key/s in $options should have attribute of selected="selected"
   * @return string HTML
   */
  public static function options($options, $selected=array()) {
    if (Dev::$narration) Dev::narrate('Generating <option>s');
    $html='';
    if (\is_string($selected)) {
      $selected = array($selected);
    }
    foreach($options AS $key => $value) {
      if (\is_array($value)) {
        $html.= "\n<optgroup label=\"$key\">";
        $html.= self::options($value);
        $html.= "\n</optgroup>";
      } else {
        $html.="\n<option value=\"$key\">$value</option>";
      }
    }
    foreach($selected AS $sel) {
      $html = str_replace("value=\"$sel\"", "value=\"$sel\" selected=\"selected\"", $html);
    }
    return $html;
  }


  /**
   * Replaces new lines with p tags
   * Similar to PHP's nl2br()
   * Does not check for empty string
   * 
   * @param string $text Body of text, possibly with line feeds
   * @return string p tags in place of the line feeds, and the whole thing wrapped with p tags
   */
  public static function paragraph($text) {
// echo preg_replace('[ {2,}]', ' ', $a);

    
    
    return "\n<p>" . implode("\n</p>\n<p>\n", explode("\n", $text)) . "\n</p>";
  }

  
  
}
